﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_ImageUploadField:PageForm_SingleInputFieldBase
    {
        private string _src = "";
        private int _width = 200;
        private int _height = 100;
        private string _uploadUrl = "";
        private int _maxSize = 0;
        private bool _disabled = false;
        private int _cropWidth = 0;
        private int _cropHeight = 0;

        public PageForm_ImageUploadField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_imageuploadfield singleImageFileUploader");
        }

        /// <summary>
        /// 图片框显示的大小
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public PageForm_ImageUploadField Size(int size)
        {
            _width = size;
            _height = size;

            return this;
        }

        /// <summary>
        /// 图片框显示的大小
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public PageForm_ImageUploadField Size(int width, int height)
        {
            _width = width;
            _height = height;

            return this;
        }

        /// <summary>
        /// 可上传的最大大小,单位为KB
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public PageForm_ImageUploadField MaxFileSize(int size)
        {
            _maxSize = size*1024;

            return this;
        }

        public PageForm_ImageUploadField Disabled(bool disabled = true)
        {
            _disabled = disabled;

            return this;
        }

        /// <summary>
        /// 强制必须裁剪图片,一般用于需要求用户按固定比例上传的情况
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public PageForm_ImageUploadField Crop(int width, int height)
        {
            _cropWidth = width;
            _cropHeight = height;
            return this;
        }

        public PageForm_ImageUploadField Src(string url)
        {
            _src = url;

            return this;
        }
        
        /// <summary>
        /// 上传地址,如果未配置,则为手动上传 (默认情况下,,选择文件后,将自动上传至指定的url中)
        /// </summary>
        /// <param name="uploadUrl">上传地址</param>
        /// <returns></returns>
        public PageForm_ImageUploadField UploadUrl(string uploadUrl)
        {
            _uploadUrl = uploadUrl;
            return this;
        }

        protected override void BeforeRender(TextWriter writer)
        {
            this.Data("uploadurl", _uploadUrl)
                .Data("maxfilesize", _maxSize.ToStringEx())
                .Data("disabled", _disabled.ToStringEx())
                .Data("name",_name)
                .Data("cropwidth", _cropWidth.ToStringEx())
                .Data("cropheight",_cropHeight.ToStringEx())
                ;


            base.BeforeRender(writer);
        }

        protected override void RenderContent(TextWriter writer)
        {
            var imageUrl = "";

            if (string.IsNullOrEmpty(_src))
            {
                if (_disabled)
                {
                    imageUrl = AdminLTEUI.LayoutPath.ResourceBaseUrl + "/Content/images/grey_80_80.png";
                }
                else
                {
                    if (_width <= _height)
                    {
                        imageUrl = AdminLTEUI.LayoutPath.ResourceBaseUrl + "/Content/images/upload_80_80.jpg";
                    }
                    else
                    {
                        imageUrl = AdminLTEUI.LayoutPath.ResourceBaseUrl + "/Content/images/upload_200_100.jpg";
                    }
                }
            }
            else
            {
                imageUrl = _src;
            }


            var errorUrl = "";

            if (_width <= _height)
            {
                errorUrl = AdminLTEUI.LayoutPath.ResourceBaseUrl + "/Content/images/picerror_80_80.jpg";
            }
            else
            {
                errorUrl = AdminLTEUI.LayoutPath.ResourceBaseUrl + "/Content/images/picerror_200_100.jpg";
            }


            writer.WriteLine($"<img class=\"uploadimage\" width=\"{_width}\" height=\"{_height}\"  src=\"{imageUrl}\" onerror=\"this.src='{errorUrl}';\"/>");

            //if (!_disabled)
            //{
            //    writer.WriteLine($"<input type=\"file\" name=\"{_uploadUrl.IfNotEmptyOrWhileSpace(_name)}\"  accept=\"image/*\" />");
            //}

            //if (!string.IsNullOrWhiteSpace(_src))
            //{
                writer.WriteLine($"<input type=\"hidden\" name=\"{_name}\" value=\"{_src}\" />");
            //}
            
        }
    }
}
